/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lineandrect;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JPanel;

/**
 *
 * @author Mr.D
 */
public class panel extends JPanel implements MouseListener, MouseMotionListener{
    //Cac so cho san
    private int xa, ya, xb, yb;
    private int xr, yr, rh, rw;


    //Cac bien dung de tinh toan
    private Random rd = new Random();
    private int a, b ,c;    
    private int holdx, holdy;
    public panel(){
        setSize(800,570);
        addMouseListener(this);
        addMouseMotionListener(this);
        init();                
    }
    private void init(){
        xa = rd.nextInt(800);
        ya = rd.nextInt(570);
        xb = rd.nextInt(800);
        yb = rd.nextInt(570);
        xr = rd.nextInt(800);
        yr = rd.nextInt(570);
        rw = rd.nextInt(800);
        rh = rd.nextInt(570);
        while((xr + rw > 800)||(yr + rh > 570)){
            xr = rd.nextInt(800);
            yr = rd.nextInt(570);
            rw = rd.nextInt(800);
            rh = rd.nextInt(570);
        }
    }
    private String caculation(){
        a = (yb - ya);
        b = (-1)*(xb - xa);
        c = ((-1)*(yb - ya)*(xa) + (xb - xa)*ya);
        if(a == 0){ 
            return (String)("y = "+ (-c/b));
        }
        else{
            if(b == 0){
                return (String)("x = " + (-c/a));
            }
            if(b > 0){
                if(c == 0)
                    return (String)(a+"x "+ b+"y = 0");
                else if(c > 0)
                       return (String)(a+"x +"+ b+"y + " + c+ " = 0");
                else
                    return (String)(a+"x +"+ b+"y " + c+ " = 0");
            }
            else{
                if(c == 0)
                    return (String)(a+"x "+ b+"y = 0");
                else if(c > 0)
                    return (String)(a+"x "+ b+"y +" + c+ " = 0");
                else
                    return (String)(a+"x "+ b+"y " + c+ " = 0");
            }
        }
    }
    private boolean check(){
        if(((xa >= xr)&&(xa <= xr + rw)&&(ya >= yr)&&(ya <= yr+rh))||((xb >= xr)&&(xb <= xr + rw)&&(yb >= yr)&&(yb <= yr+rh))){
            return true;
        }
        else{
            if(a == 0){
                if((((xr >= xa)&&(xr <= xb))||((xr >= xb)&&(xr <= xa)))&&((yr >= ya)&&(yr <= yb)))
                    return true;
                else if(((xr + rw >= xa)&&(xr+rw<= xb))||((xr + rw >= xb)&&(xr + rw <= xa))&&((yr >= ya)&&(yr <= yb)))
                    return true;
                else
                    return false;
            }
            else if(b == 0){
                if((((yr >= ya)&&(yr <= yb))||((yr >= yb)&&(yr <= ya))||((yr+rh >= ya)&&(yr + rh <= yb))||((yr + rh >= yb)&&(yr +rh <= ya)))&&((xr >= xa)&&(xr <= xb)))
                    return true;
                
                else
                    return false;
            }
            else{
                     if(((-a * xr-c)/b >= yr)&&((-a * xr-c)/b <= yr + rh)&&(((xr >= xa)&&(xr<= xb))||((xr >= xb)&&(xr <= xa))))
                     return true;
                else if(((-a * (xr+rw)-c)/b >= yr)&&((-a * (xr+rw)-c)/b <= yr + rh)&&(((xr+rw >= xa)&&(xr+rw<= xb))||((xr+rw >= xb)&&(xr+rw <= xa))))
                    return true;
                else if(((-b*yr-c)/a >= xr)&&((-b*yr-c)/a<= xr+ rw)&&(((yr >= ya)&&(yr <= yb))||((yr >= yb)&&(yr <= ya))))
                    return true;
                else if(((-b*(yr+rh)-c)/a >= xr)&&((-b*(yr+rh)-c)/a <= xr+ rw)&&(((yr+rh >= ya)&&(yr+rh <= yb))||((yr+rh >= yb)&&(yr+rh <= ya))))
                    return true;
                else
                    return false;
            }
        }
    }
    @Override
    public void paintComponent(Graphics g){
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 800, 570);
        g.setColor(Color.BLACK);
        g.drawLine(0, 0, 800, 0);
        g.drawLine(0, 0, 0, 570);
        g.drawString("O", 2, 12);
        g.drawString("y", 2, 510);
        g.drawString("x", 752, 10);
        g.drawString("►", 765, 7);
        g.drawString("▼", -5, 530);
        g.setColor(Color.BLACK);
        g.drawLine(xa, ya, xb, yb);
        g.drawRect(xr, yr, rw, rh);
        g.drawString("A("+xa+", "+ya+")", xa + 5, ya-5);
        g.drawString("B("+xb+", "+yb+")", xb + 5, yb-5);
        g.drawString("("+xr+", "+yr+")", xr + 5, yr + 15);
        g.drawString("Height = "+rh, xr + 5, yr +rh/2-20);
        g.drawString("Width = "+rw, xr +rw/2, yr + 15);
        g.drawString("Phương trình đường thẳng: "+caculation(), 20, 510);
        if(check() == true)
            g.drawString("Kiểm tra: Cắt", 20, 530);
        else
            g.drawString("Kiểm tra: Không Cắt", 20, 530);
        g.drawString("Giữ cạnh hcn để di chuyển, giữ góc dưới phải hcn để thay đổi kích thước !", 380, 510);
        g.drawString("Click để vẽ hình ngẫu nhiên, click và giữ để vẽ đường thẳng !", 380, 530);
    }

    public void mouseClicked(MouseEvent e) {
        xa = rd.nextInt(800);
        ya = rd.nextInt(570);
        xb = rd.nextInt(800);
        yb = rd.nextInt(570);
        xr = rd.nextInt(800);
        yr = rd.nextInt(570);
        rw = rd.nextInt(800);
        rh = rd.nextInt(570);
        while((xr + rw > 800)||(yr + rh > 570)){
            xr = rd.nextInt(800);
            yr = rd.nextInt(570);
            rw = rd.nextInt(800);
            rh = rd.nextInt(570);
        }
        repaint();
    }

    public void mousePressed(MouseEvent e) {
        holdx = e.getX();
        holdy = e.getY();        
        xa = holdx;
        ya = holdy;
    }

    public void mouseReleased(MouseEvent e) {
        xb = e.getX();
        yb = e.getY();
    }

    public void mouseEntered(MouseEvent e) {
        
    }

    public void mouseExited(MouseEvent e) {
        
    }

    public void mouseDragged(MouseEvent e) {
        if((holdx == xr)&&(holdy >= yr)&&(holdy <= yr+rh)){
            xr = xr + e.getX() - holdx;
            yr = yr + e.getY() - holdy;
            holdx = e.getX();
            holdy = e.getY();
        }
        else if((holdx == xr + rw)&&(holdy >= yr)&&(holdy <= yr+rh)){
            xr = xr + e.getX() - holdx;
            yr = yr + e.getY() - holdy;
            holdx = e.getX();
            holdy = e.getY();
        }
        else if((holdy == yr)&&(holdx >= xr)&&(holdx<= xr+rw)){
            xr = xr + e.getX() - holdx;
            yr = yr + e.getY() - holdy;
            holdx = e.getX();
            holdy = e.getY();
        }
        else if((holdy == yr+rh)&&(holdx >= xr)&&(holdx<= xr+rw)){
            xr = xr + e.getX() - holdx;
            yr = yr + e.getY() - holdy;
            holdx = e.getX();
            holdy = e.getY();
        }
        else if((holdx > xr + rw-2)&&(holdx < xr + rw+2)&&(holdy > yr + rh - 2)&&(holdy < yr + rh +2)){
            rw = rw + e.getX() - holdx;
            rh = rh + e.getY() - holdy;
            holdx = e.getX();
            holdy = e.getY();
        }
        else{
            xb = e.getX();
            yb = e.getY();
        }
        repaint();
    }

    public void mouseMoved(MouseEvent e) {
    }

}
